/*   1:    */ package app.sso;
/*   2:    */ 
/*   3:    */ import com.seeyon.ctp.common.AppContext;
/*   4:    */ import com.seeyon.ctp.common.authenticate.TrustAddressManager;
/*   5:    */ import com.seeyon.ctp.common.authenticate.sso.SSOTicketManager;
/*   6:    */ import com.seeyon.ctp.organization.principal.PrincipalManager;
/*   7:    */ import com.seeyon.ctp.portal.sso.SSOLoginContext;
/*   8:    */ import com.seeyon.ctp.portal.sso.SSOLoginContextManager;
/*   9:    */ import com.seeyon.ctp.portal.sso.SSOLoginHandshakeInterface;
/*  10:    */ import com.seeyon.ctp.signin.manager.SignInManager;
/*  11:    */ import com.seeyon.ctp.signin.po.SignIn;
/*  12:    */ import com.seeyon.ctp.usersystem.manager.RestUserManager;
/*  13:    */ import com.seeyon.ctp.usersystem.po.RestUser;
/*  14:    */ import com.seeyon.v3x.services.security.ServiceManager;
/*  15:    */ import java.io.IOException;
/*  16:    */ import java.io.PrintStream;
/*  17:    */ import java.io.PrintWriter;
/*  18:    */ import java.net.URLEncoder;
/*  19:    */ import java.util.Map;
/*  20:    */ import javax.servlet.ServletException;
/*  21:    */ import javax.servlet.http.HttpServlet;
/*  22:    */ import javax.servlet.http.HttpServletRequest;
/*  23:    */ import javax.servlet.http.HttpServletResponse;
/*  24:    */ import org.apache.commons.logging.Log;
/*  25:    */ import org.apache.commons.logging.LogFactory;
/*  26:    */ import org.springframework.util.StringUtils;
/*  27:    */ 
/*  28:    */ public class SSOLoginServlet
/*  29:    */   extends HttpServlet
/*  30:    */ {
/*  31: 29 */   private static final Log log = LogFactory.getLog(SSOLoginServlet.class);
/*  32:    */   private transient Object object;
/*  33:    */   private transient PrincipalManager principalManager;
/*  34:    */   private SignInManager ssoManager;
/*  35:    */   private static final long serialVersionUID = 1L;
/*  36:    */   
/*  37:    */   public SSOLoginServlet()
/*  38:    */   {
/*  39: 37 */     this.object = new Object();
/*  40: 38 */     this.principalManager = null;
/*  41: 39 */     this.ssoManager = null;
/*  42:    */   }
/*  43:    */   
/*  44:    */   public void setSignInManager(SignInManager signInManager)
/*  45:    */   {
/*  46: 43 */     this.ssoManager = signInManager;
/*  47:    */   }
/*  48:    */   
/*  49:    */   public SignInManager getSignInManager()
/*  50:    */   {
/*  51: 47 */     return this.ssoManager;
/*  52:    */   }
/*  53:    */   
/*  54:    */   protected void doGet(HttpServletRequest req, HttpServletResponse resp)
/*  55:    */     throws ServletException, IOException
/*  56:    */   {
/*  57: 55 */     if (this.principalManager == null) {
/*  58: 56 */       this.principalManager = ((PrincipalManager)AppContext.getBean("principalManager"));
/*  59:    */     }
/*  60: 57 */     if (this.ssoManager == null) {
/*  61: 58 */       this.ssoManager = ((SignInManager)AppContext.getBean("signInManager"));
/*  62:    */     }
/*  63: 59 */     req.setCharacterEncoding("UTF-8");
/*  64: 60 */     resp.setContentType("text/html; charset=UTF-8");
/*  65: 61 */     PrintWriter out = resp.getWriter();
/*  66: 62 */     SSOLoginContext ssoLoginContext = null;
/*  67: 63 */     String from = req.getParameter("from");
/*  68: 64 */     String signin = req.getParameter("signin");
/*  69:    */     
/*  70: 66 */     String token = req.getParameter("token");
/*  71: 67 */     if (!isBlank(token))
/*  72:    */     {
/*  73: 69 */       String loginName = (String)ServiceManager.getTokenSessionMap().get(token);
/*  74: 70 */       RestUserManager restUserManager = (RestUserManager)AppContext.getBean("restUserManager");
/*  75: 71 */       RestUser user = restUserManager.getUserByLoginName(loginName);
/*  76: 72 */       System.out.println("token转rest user：" + user);
/*  77:    */     }
/*  78:    */     else
/*  79:    */     {
/*  80: 80 */       out.println("Token is null.");
/*  81: 81 */       return;
/*  82:    */     }
/*  83: 84 */     if ((!isBlank(signin)) && (isBlank(from)))
/*  84:    */     {
/*  85: 85 */       ssoLoginContext = signinTo(signin);
/*  86:    */     }
/*  87: 86 */     else if ((isBlank(signin)) && (!isBlank(from)))
/*  88:    */     {
/*  89: 87 */       ssoLoginContext = SSOLoginContextManager.getInstance().getSSOLoginContext(from);
/*  90:    */     }
/*  91: 88 */     else if ((isBlank(signin)) && (isBlank(from)))
/*  92:    */     {
/*  93: 89 */       out.println("Parameter 'from' is not available.");
/*  94: 90 */       resp.addHeader("SSOError", "Parameter 'from' is not available.");
/*  95: 91 */       return;
/*  96:    */     }
/*  97: 93 */     if (ssoLoginContext == null)
/*  98:    */     {
/*  99: 94 */       out.println(from + " is not available.");
/* 100: 95 */       resp.addHeader("SSOError", 
/* 101: 96 */         from + " is not available.");
/* 102: 97 */       return;
/* 103:    */     }
/* 104: 99 */     if ((ssoLoginContext.isEnableTrustAddress()) && (!TrustAddressManager.getInstance().isTrustPass(req)))
/* 105:    */     {
/* 106:100 */       out.println(from + " client ip is untrusted.");
/* 107:101 */       resp.addHeader("SSOError", 
/* 108:102 */         from + " client ip is untrusted.");
/* 109:103 */       return;
/* 110:    */     }
/* 111:105 */     String ticketName = ssoLoginContext.getTicketName();
/* 112:106 */     String ticket = req.getParameter(ticketName);
/* 113:107 */     if (isBlank(ticket))
/* 114:    */     {
/* 115:108 */       out.println("Parameter '" + ticketName + "' is not available.");
/* 116:109 */       resp.addHeader("SSOError", 
/* 117:110 */         "Parameter '" + ticketName + "' is not available.");
/* 118:111 */       return;
/* 119:    */     }
/* 120:113 */     SSOLoginHandshakeInterface handshake = ssoLoginContext.getHandshake();
/* 121:114 */     if (handshake == null)
/* 122:    */     {
/* 123:115 */       out.println("SSOLoginHandshakeInterface is not implements.");
/* 124:116 */       resp.addHeader("SSOError", "Parameter 'SSOLoginHandshakeInterface is not implements.");
/* 125:117 */       return;
/* 126:    */     }
/* 127:119 */     String username = null;
/* 128:    */     try
/* 129:    */     {
/* 130:121 */       username = handshake.handshake(ticket);
/* 131:    */     }
/* 132:    */     catch (Throwable e)
/* 133:    */     {
/* 134:123 */       log.error(ssoLoginContext, e);
/* 135:124 */       out.println("Login fail");
/* 136:125 */       resp.addHeader("SSOError", e.getMessage());
/* 137:126 */       return;
/* 138:    */     }
/* 139:    */     try
/* 140:    */     {
/* 141:129 */       String tourl = req.getParameter("tourl");
/* 142:130 */       SSOTicketManager.getInstance().newTicketInfo(ticket, username, from);
/* 143:131 */       StringBuilder sb = null;
/* 144:132 */       synchronized (this.object)
/* 145:    */       {
/* 146:133 */         sb = get2URL(req, ssoLoginContext, ticket, tourl);
/* 147:    */       }
/* 148:135 */       resp.sendRedirect(sb.toString());
/* 149:    */     }
/* 150:    */     catch (Throwable e)
/* 151:    */     {
/* 152:137 */       out.println(e.getMessage());
/* 153:    */     }
/* 154:    */   }
/* 155:    */   
/* 156:    */   private SSOLoginContext signinTo(String param)
/* 157:    */   {
/* 158:142 */     SignIn ssoInfo = this.ssoManager.findSsoByName(param);
/* 159:143 */     SSOLoginHandshakeInterface handshake = null;
/* 160:    */     try
/* 161:    */     {
/* 162:145 */       handshake = 
/* 163:146 */         (SSOLoginHandshakeInterface)Class.forName(ssoInfo.getCheckUrl() == null ? null : ssoInfo.getCheckUrl()).newInstance();
/* 164:    */     }
/* 165:    */     catch (InstantiationException e)
/* 166:    */     {
/* 167:148 */       e.printStackTrace();
/* 168:    */     }
/* 169:    */     catch (IllegalAccessException e)
/* 170:    */     {
/* 171:150 */       e.printStackTrace();
/* 172:    */     }
/* 173:    */     catch (ClassNotFoundException e)
/* 174:    */     {
/* 175:152 */       e.printStackTrace();
/* 176:    */     }
/* 177:154 */     SSOLoginContext signin = new SSOLoginContext();
/* 178:155 */     signin.setHandshake(handshake);
/* 179:156 */     signin.setForward(ssoInfo.getSort().intValue() != 0);
/* 180:157 */     signin.setTicketName(ssoInfo.getLoginParam() == null ? "ticket" : ssoInfo.getLoginParam());
/* 181:158 */     return signin;
/* 182:    */   }
/* 183:    */   
/* 184:    */   private StringBuilder get2URL(HttpServletRequest req, SSOLoginContext ssoLoginContext, String ticket, String tourl)
/* 185:    */   {
/* 186:163 */     StringBuilder sb = new StringBuilder();
/* 187:164 */     sb.append("/seeyon/main.do?method=login&ticket=" + URLEncoder.encode(ticket) + 
/* 188:165 */       "&ssoFrom=" + req.getParameter("from")).append("&login.destination=").append(URLEncoder.encode(req.getParameter("dest")).toString());
/* 189:166 */     sb = urlMosaic(req, ssoLoginContext, tourl, sb);
/* 190:167 */     return sb;
/* 191:    */   }
/* 192:    */   
/* 193:    */   private StringBuilder urlMosaic(HttpServletRequest req, SSOLoginContext ssoLoginContext, String tourl, StringBuilder sb)
/* 194:    */   {
/* 195:172 */     String UserLanguage = req.getParameter("login_locale");
/* 196:173 */     if (UserLanguage != null) {
/* 197:174 */       sb.append("&login_locale=" + UserLanguage);
/* 198:    */     }
/* 199:175 */     String UserAgentFrom = req.getParameter("UserAgentFrom");
/* 200:176 */     if (UserAgentFrom != null) {
/* 201:177 */       sb.append("&UserAgentFrom=" + UserAgentFrom);
/* 202:    */     }
/* 203:178 */     String des = tourl == null ? ssoLoginContext.getDestination() : tourl;
/* 204:179 */     if (StringUtils.hasText(des))
/* 205:    */     {
/* 206:180 */       sb.append("&");
/* 207:181 */       sb.append("login.destination");
/* 208:182 */       sb.append("=");
/* 209:183 */       sb.append(URLEncoder.encode(des));
/* 210:184 */       String top = ssoLoginContext.getTopFrameName();
/* 211:185 */       if (StringUtils.hasText(top))
/* 212:    */       {
/* 213:186 */         sb.append("&");
/* 214:187 */         sb.append("com.seeyon.sso.topframename");
/* 215:188 */         sb.append("=");
/* 216:189 */         sb.append(top);
/* 217:    */       }
/* 218:    */     }
/* 219:192 */     return sb;
/* 220:    */   }
/* 221:    */   
/* 222:    */   protected void doPost(HttpServletRequest req, HttpServletResponse resp)
/* 223:    */     throws ServletException, IOException
/* 224:    */   {
/* 225:196 */     doGet(req, resp);
/* 226:    */   }
/* 227:    */   
/* 228:    */   private boolean isBlank(String s)
/* 229:    */   {
/* 230:200 */     return (s == null) || (s.length() == 0);
/* 231:    */   }
/* 232:    */ }


/* Location:           C:\Users\GNB543\Desktop\classes\
 * Qualified Name:     app.sso.SSOLoginServlet
 * JD-Core Version:    0.7.0.1
 */